$().ready(function() {
	var oldStock = 0;
	loadType();
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/wx/goodsBargain/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			price : {
				required : true,
				min : 0
			},
			stock : {
				required : true,
				max : oldStock,
				min : 0
			},
			rebateOne : {
				required : true,
				min : 0
			},
			rebateTwo : {
				required : true,
				min : 0
			},
			minPrice : {
				required : true,
				min : 0
			},
			maxPrice : {
				required : true,
				min : 0
			},
			time : {
				required : true,
				min : 0
			}
		},
		messages : {
			price : {
				required : icon + "请输入价格",
				min : icon + "最小为0"
			},
			stock : {
				required : icon + "请输入库存",
				max : icon + "库存太大",
				min : icon + "最小为0"
			},
			rebateOne : {
				required : icon + "请输入返利",
				min : icon + "最小为0"
			},
			rebateTwo : {
				required : icon + "请输入返利",
				min : icon + "最小为0"
			},
			minPrice : {
				required : icon + "请输入金额",
				min : icon + "最小为0"
			},
			maxPrice : {
				required : icon + "请输入金额",
				min : icon + "最小为0"
			},
			time : {
				required : icon + "请输入时间",
				min : icon + "最小为0"
			}
		}
	})
}

function loadType(){
	console.log("加载商品--------------------")
	var html = "";
	$.ajax({
		url : '/wx/goodsGoods/getGoods',
		success : function(data) {
			// console.log(data);
			data = data.data;
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option data-price="' + data[i].price + '" data-stock="' + data[i].stock + '" value="' + data[i].id + '">' + data[i].name + '</option>';
				// console.log(data[i])
			}
			// console.log("循环结束");
			$(".chosen-select").append(html);
			$(".chosen-select").chosen({
				maxHeight : 200
			});
			//改变事件
			$('.chosen-select').on('change', function(e, params) {
				var se = $('.chosen-select option:selected');
				console.log("goodsId: "+params.selected);
				console.log("price: "+se.attr("data-price"));
				console.log("stock: "+se.attr("data-stock"));
				$('#one').css("display","");
				$('#two').css("display","");
				$('#oldPrice').val(se.attr("data-price"));
				$('#oldStock').val(se.attr("data-stock"));
				oldStock = se.attr("data-stock");
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}